package cn.itsource.pethome.Basic.service.Impl;

import cn.itsource.pethome.Basic.mapper.BaseMapper;
import cn.itsource.pethome.Basic.service.IBaseService;
import cn.itsource.pethome.Basic.util.PageList;
import cn.itsource.pethome.org.query.BaseQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;
    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<T> loadAll() {
        return baseMapper.loadAll();
    }
    /**
     * 根据条件查询数据列表
     * @param query
     * @return
     */
    @Override
    public PageList<T> loadAllByQuery(BaseQuery query) {
        Long total = baseMapper.findCount(query);
        if(total.equals(0L)){
            return new PageList<T>();
        }
        List<T> data = baseMapper.loadAllByQuery(query);
        return new PageList<>(data,total);
    }
    /**
     * 增加数据
     * @param t 数据
     * @return
     */
    @Override
    @Transactional
    public void add(T t) {
        baseMapper.add(t);
    }
    /**
     * 删除数据
     * @param id
     */
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.delete(id);
    }
    /**
     * 批量删除数据
     * @param ids
     */
    @Override
    @Transactional
    public void batchRemove(Long[] ids) {
        baseMapper.batchRemove(ids);
    }
    /**
     * 修改数据
     * @param t
     */
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }
}
